package com.huawei.petapp2.impl;

import android.text.TextUtils;

import com.huawei.petapp2.activity.widget.App;
import com.huawei.petapp2.entity.PeronalInfoBean;
import com.huawei.petapp2.utils.ACache;

import java.io.Serializable;

/**
 * 作者： ch
 * 时间： 2018/6/26 0026-下午 4:23
 * 描述：
 * 来源：
 */


public class UserImpl {

    private static final String USER_CACHE = "USER_CACHE";


    /**
     * 获取缓存的对象
     *
     * @return
     */
    public static UserImplModel getUserModel() {
        ACache cache = ACache.get(App.getContext());
        UserImplModel implModel = (UserImplModel) cache.getAsObject(USER_CACHE);
        return implModel;
    }

    /**
     * 保存
     *
     * @param model
     */
    public static void saveCartModel(UserImplModel model) {
        ACache cache = ACache.get(App.getContext());
        cache.put(USER_CACHE, model);
    }

    public static void clearUserImpl() {
        saveCartModel(null);
    }

    /**
     * 更新用户默认地址id
     *
     * @param addressId
     */

    public static void upDataAddressId(String addressId) {
        UserImplModel userImplModel = getUserModel();

        if (userImplModel != null && userImplModel.getInfoModel() != null) {
            userImplModel.getInfoModel().setAddress_id(addressId);
        }
        saveCartModel(userImplModel);
    }

    /**
     * 更新用户默认地址id
     */

    public static String getAddressId() {
        UserImplModel userImplModel = getUserModel();

        if (userImplModel != null && userImplModel.getInfoModel() != null) {
            return userImplModel.getInfoModel().getAddress_id();
        }
        return "";
    }

    /**
     * 获取用户余额
     *
     * @return
     */
    public static String getUserMoney() {
        UserImplModel userImplModel = getUserModel();

        if (userImplModel != null && userImplModel.getInfoModel() != null) {

            return userImplModel.getInfoModel().getUser_money();
        }
        return "";
    }

    /**
     * 获取用户辅销币余额
     *
     * @return
     */
    public static String getUserFxp_money() {
        UserImplModel userImplModel = getUserModel();

        if (userImplModel != null && userImplModel.getInfoModel() != null) {

            return userImplModel.getInfoModel().getFxp_money();
        }
        return "";
    }

    /**
     * 获取用户代金券
     *
     * @return
     */
    public static String getUserFxp_points() {
        UserImplModel userImplModel = getUserModel();

        if (userImplModel != null && userImplModel.getInfoModel() != null) {

            return userImplModel.getInfoModel().getFxp_points();
        }
        return "";
    }


    public static boolean isUpLevel() {
        UserImplModel userImplModel = getUserModel();
        if (userImplModel != null && userImplModel.getInfoModel() != null) {

            if (getUserLevel() != null && getUserLevel().equals("4")) {
                //已经是合伙人 不能升级
                return false;
            }
            //这两个时间都存在且不相等时不能进行二次升级
            if (!TextUtils.isEmpty(userImplModel.getInfoModel().getACTIVATION_TIME()) &&
                    !TextUtils.isEmpty(userImplModel.getInfoModel().getDistribution_save_time())) {

                if (!userImplModel.getInfoModel().getACTIVATION_TIME().equals(userImplModel.getInfoModel().getDistribution_save_time())) {
                    return false;
                }
            }
        }


        return true;
    }


    public static String getUserId() {

        UserImplModel model = getUserModel();

        if (model != null) {
            return model.getUser_id();
        }
        return "";
    }

    public static String getUserName() {
        UserImplModel model = getUserModel();
        if (model != null) {
            return model.getUser_name();
        }
        return "";
    }

    public static String getAppToken() {
        UserImplModel model = getUserModel();
        if (model != null) {
            if (model.getApp_token() == null) {
                return "";
            }
            return model.getApp_token();
        }
        return "";
    }

    public static String getUserLevel() {
        UserImplModel model = getUserModel();
        if (model != null) {
            return model.getDistribution_level();
        }
        return "";
    }

    public static String getUserImg() {
        UserImplModel model = getUserModel();
        if (model != null) {
            return model.getUser_img();
        }
        return "";
    }


    public static class UserImplModel implements Serializable {
        private PeronalInfoBean infoModel;
        private String app_token;
        private String user_id;
        private String user_name;
        private String discount;
        private String distribution_level_name;
        private String money;
        private String distribution_level;
        private String user_img;
        private String integral_ratio;

        public String getIntegral_ratio() {
            return integral_ratio;
        }

        public void setIntegral_ratio(String integral_ratio) {
            this.integral_ratio = integral_ratio;
        }

        public String getUser_img() {
            return user_img;
        }

        public void setUser_img(String user_img) {
            this.user_img = user_img;
        }


        public String getDiscount() {
            return discount;
        }

        public void setDiscount(String discount) {
            this.discount = discount;
        }

        public String getDistribution_level_name() {
            return distribution_level_name;
        }

        public void setDistribution_level_name(String distribution_level_name) {
            this.distribution_level_name = distribution_level_name;
        }

        public String getMoney() {
            return money;
        }

        public void setMoney(String money) {
            this.money = money;
        }


        public PeronalInfoBean getInfoModel() {
            return infoModel;
        }

        public void setInfoModel(PeronalInfoBean infoModel) {
            this.infoModel = infoModel;
        }

        public String getApp_token() {
            return app_token;
        }

        public void setApp_token(String app_token) {
            this.app_token = app_token;
        }

        public String getUser_id() {
            return user_id;
        }

        public void setUser_id(String user_id) {
            this.user_id = user_id;
        }

        public String getUser_name() {
            return user_name;
        }

        public void setUser_name(String user_name) {
            this.user_name = user_name;
        }

        public String getDistribution_level() {
            return distribution_level;
        }

        public void setDistribution_level(String distribution_level) {
            this.distribution_level = distribution_level;
        }

    }


}
